package com.xuzz.study.io.nio;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;

public class NioClient {
    public static void main(String[] args) throws Exception {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);
        if(!socketChannel.connect(new InetSocketAddress("127.0.0.1",6666)))
        {
            //如果成功
            while(!socketChannel.finishConnect())
            {
                System.out.println("因为连接需要时间，客户端不会阻塞，可以做一些其他工作");
            }
        }
        Scanner input = new Scanner(System.in);
        System.out.println("请输入一个字符串(中间能加空格或符号)");
        while(true)
        {
            String a = input.nextLine();
            if("q".equals(a.toLowerCase()))
            {
                break;
            }
            ByteBuffer byteBuffer = ByteBuffer.wrap(a.getBytes());
            socketChannel.write(byteBuffer);
        }
    }
}
